<template>
  <div class="top">
    <div class="top__header">
      <span class="top__header__back iconfont" @click="cartBack">&#xe6db;</span>
      确认订单
    </div>
    <div class="top__receiver" @click="handleAddressClick">
      <div class="top__receiver__title">收货地址</div>
      <div class="top__receiver__address" v-if="isAddress">
        {{ item.city }}{{ item.department }}{{ item.houseNumber }}
      </div>
      <div class="top__receiver__address" v-else>请先选择收货地址</div>
      <div class="top__receiver__info" v-if="isAddress">
        <span class="top__receiver__info__name">{{ item.name }}</span>
        <span class="top__receiver__info__name">{{ item.phone }}</span>
      </div>
      <div class="top__receiver__enter iconfont">&#xe6aa;</div>
    </div>
  </div>
</template>

<script>
import { useRouter, useRoute } from "vue-router";
import { get } from "../../utils/request";
import { reactive, toRefs } from "vue";
/**
 * 处理获取地址数据和页面跳转
 */
const useClickEffect = () => {
  const router = useRouter();
  const route = useRoute();
  /**
   * 地址数据
   */
  let address = reactive({
    item: {},
  });
  /**
   * 是否获取了地址数据
   */
  const isAddress = route.query.addressId || "";
  /**
   * 获取页面路径
   */
  const path = route.path;
  /**
   * 获取地址数据
   */
  const getItem = async () => {
    const { data } = await get(`/api/user/address/${isAddress}`);
    console.log(data);
    address.item = data.data;
  };
  //返回上个页面
  const cartBack = () => {
    router.back();
  };
  //点击获取地址数据
  const handleAddressClick = () => {
    router.push(`/address?path=${path}`);
  };
  //将地址数据解构出来
  const { item } = toRefs(address);
  return { cartBack, isAddress, handleAddressClick, item, getItem };
};
export default {
  name: "orderTop",
  setup() {
    const { cartBack, isAddress, handleAddressClick, item, getItem } =
      useClickEffect();
    getItem();

    return { cartBack, isAddress, handleAddressClick, item };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
.top {
  height: 1.82rem;
  background-size: 100% 1.59rem;
  background-image: linear-gradient(0deg, rgba(0, 145, 255, 0) 4%, #0091ff 50%);
  background-repeat: no-repeat;
  box-sizing: border-box;
  &__header {
    position: relative;
    padding-top: 0.26rem;
    font-size: 0.16rem;
    line-height: 0.22rem;
    color: $color-fff;
    text-align: center;
    &__back {
      position: absolute;
      left: 0.18rem;
      font-size: 0.2rem;
    }
  }
  &__receiver {
    position: relative;
    margin: 0.22rem 0.18rem 0;
    height: 1.11rem;
    background-color: $color-fff;
    border-radius: 0.04rem;
    box-sizing: border-box;
    padding: 0.16rem;
    &__title {
      font-size: 0.16rem;
      color: $content-fontcolor;
      font-weight: 600;
      line-height: 0.2rem;
    }
    &__address {
      margin-top: 0.14rem;
      font-size: 0.14rem;
      line-height: 0.2rem;
      color: $content-fontcolor;
    }
    &__info {
      margin-top: 0.06rem;
      font-size: 0.12rem;
      color: $color-666;
      &__name {
        margin-right: 0.06rem;
        line-height: 0.18rem;
      }
    }
    &__enter {
      position: absolute;
      right: 0.16rem;
      top: 50%;
      transform: translateY(-50%);
      font-size: 0.2rem;
      color: $color-666;
    }
  }
}
</style>